/*
 * CS_REQ_MOVE_TO_INVEN.cpp
 *
 *  Created on: 2013. 10. 18.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_MOVE_TO_INVEN(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	uint64_t itemSeq = 0;

	*pPacket >> itemSeq;

	NLogic::Bag * pWareHouseBag = pUser->GetBag(BAG_TYPE_WAREHOUSE);
	if (NULL == pWareHouseBag)
	{
		DEBUGLOG("CS_REQ_MOVE_TO_INVEN : pWareHouseBag is NULL!!");
		return;
	}

	NLogic::Item * pItem = pWareHouseBag->GetItem(itemSeq);
	if (NULL == pItem)
	{
		DEBUGLOG("CS_REQ_MOVE_TO_INVEN : pItem is NULL!! itemseq=%lld", itemSeq);
		return;
	}

	pItem->ChangeBagTypeFromResourceData();

	NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(pItem->GetData().bag_type));
	if (NULL == pBag)
	{
		DEBUGLOG("CS_REQ_MOVE_TO_INVEN : pBag is NULL!! bagtype=%lld", pItem->GetData().bag_type);
		return;
	}

	if (true == pBag->IsBagFull())
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_INVEN_BAG_FULL);
		pAck->MakePacket(CMD_SC_REQ_MOVE_TO_INVEN);
		pUser->Send(pAck);
		return;
	}

	// 인벤에 아이템 삽입
	pBag->AddItem(pItem);

	// 창고에서 아아템 제거
	pWareHouseBag->PopItem(itemSeq);
	pItem->UpdateDatabaseItemInfo();

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_REQ_MOVE_TO_INVEN);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/


